package com.rf.richfitwheel.admin.executor.service.jobhandler;

import com.rf.richfitwheel.admin.basedata.service.BasicDataOrgService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class BasicDataSynJobHandler {
    private Logger log = LoggerFactory.getLogger(BasicDataSynJobHandler.class);

    @Autowired
    private BasicDataOrgService basicDataOrgService;

    @XxlJob("basicDataOrgSynHandler")
    public ReturnT<String> basicDataOrgSynHandler(String param){
        try{
            param = XxlJobHelper.getJobParam();
            //入库时间
            long myDate = new Date().getTime();
            log.info("开始执行组织机构数据同步................");
            long start = System.currentTimeMillis();
            //同步数据总条数
            Integer countSyn = 0;
            //调用接口同步数据并入库
            countSyn = basicDataOrgService.orgSynDo(myDate, countSyn);
            long end = System.currentTimeMillis();
            log.info("执行组织机构数据同步完成，共同步{}条数据，用时{}秒................", countSyn, (end-start)/1000);
            //处理入库后的数据
            log.info("开始执行组织机构数据更新................");
            long start1 = System.currentTimeMillis();
            basicDataOrgService.orgDeal(myDate);
            long end1 = System.currentTimeMillis();
            log.info("执行组织机构数据更新完成，共用时{}秒................", (end1-start1)/1000);
            return new ReturnT<>(HttpStatus.OK.value(), "执行成功，共同步" + countSyn + "条数据，用时" + (end-start)/1000 + "秒。执行数据更新成功共用时" + (end1-start1)/1000 + "秒。");
        }catch(Exception e){
            log.error("执行失败", e);
            return new ReturnT<>(ReturnT.FAIL_CODE, "" + e);
        }
    }

    /**
     * 组织机构同步任务，已入库的情况下进行处理
     *
     * @param param 参数
     * @return 任务执行结果
     */
    @XxlJob("basicDataOrgManageHandler")
    public ReturnT<String> basicDataOrgManageHandler(String param){
        try{
            param = XxlJobHelper.getJobParam();
            JSONObject pj = JSONObject.fromObject(param);
            //入库时间
            long myDate = pj.getLong("myDate");
            //处理入库后的数据
            log.info("开始执行组织机构数据更新................");
            long start1 = System.currentTimeMillis();
            basicDataOrgService.orgDeal(myDate);
            long end1 = System.currentTimeMillis();
            log.info("执行组织机构数据更新完成，共用时{}秒................", (end1-start1)/1000);
            return new ReturnT<>(HttpStatus.OK.value(), "执行成功，用时" + (end1-start1)/1000 + "秒。");
        }catch(Exception e){
            log.error("执行失败", e);
            return new ReturnT<>(ReturnT.FAIL_CODE, "" + e);
        }
    }

    /**
     * 清理同步的机构历史数据
     * @param param 参数
     * @return 清理结果
     */
    @XxlJob("clearBasicDataOrgHandler")
    public ReturnT<String> clearBasicDataOrg(String param){
        try {
            long start = System.currentTimeMillis();
            //调用接口同步数据并入库
            basicDataOrgService.clearBasicDataOrg();
            long end = System.currentTimeMillis();
            log.info("清理机构历史数据成功，共用时{}秒................", (end-start)/1000);
            return new ReturnT<>(HttpStatus.OK.value(), "清理机构历史数据成功，用时" + (end-start)/1000 + "秒。");
        } catch (Exception e) {
            log.error("执行失败", e);
            return new ReturnT<>(ReturnT.FAIL_CODE, "" + e);
        }
    }
}
